/**
 * 
 */
$(document).ready(
		function() {

			$('body').on('click', 'button.unprocessed-order', function(event) {

				var link = $(this).data('url');
				window.location.href = link;

				/*
				 * var $modal = getModalSkeleton(); //
				 * BLCAdmin.showLinkAsModal(link);
				 * $modal.addClass('loading-modal'); $modal.find('.modal-header
				 * h3').text(BLCAdmin.messages.loading);
				 * $modal.find('.modal-body').append($('<i>', { 'class' :
				 * 'icon-spin icon-spinner' }));
				 * $modal.find('.modal-body').css('text-align',
				 * 'center').css('font-size', '24px').css('padding-bottom',
				 * '15px'); // BLCAdmin.showElementAsModal($modal);
				 * $('body').append($modal);
				 * 
				 * //Then replace it with the actual requested link //
				 * BLCAdmin.modalNavigateTo(link); $modal.data('initialized',
				 * 'false'); BLC.ajax({ url : link, type : "GET" },
				 * function(data) { // Create a modal out of the server response
				 * var $data = $(data);
				 * 
				 * $data = $data.children(); $modal.empty().append($data);
				 * 
				 * BLCAdmin.initializeModalTabs($modal);
				 * BLCAdmin.initializeModalButtons($modal);
				 * BLCAdmin.setModalMaxHeight($modal);
				 * BLCAdmin.initializeFields();
				 * 
				 * $modal.removeClass('loading-modal');
				 * $('.section-container').html($data); });
				 */

				return false;
			});

			$('body').on('click', '#send-bulkemail', function(event) {

				$("#send-bulkemail").attr("value", "发送中，请等待");
				$("#send-bulkemail").attr("disabled", true);
				$("#sendEmailForm").submit();

				return false;
			});

			$('body').on('click', 'button.copy-a-product', function(event) {

				var link = $(this).data('url');
				var $form = BLCAdmin.getForm($(this));
				var currentAction = $form.attr('action');
				var copyUrl = currentAction + '/copy'

				BLCAdmin.showLinkAsModal(copyUrl);

				return false;
			});

			/*******************************************************************
			 * 批量上下架操作
			 ******************************************************************/
			$('body').on('click', 'button.update-active-date', function(event) {
				var url = $(this).data('url');
				BLCAdmin.showLinkAsModal(url);
			});
			/*******************************************************************
			 * 批量上架执行逻辑
			 ******************************************************************/
			$('body').on(
					'click',
					'button.confirm-update-active-date',
					function(event) {
						var $this = $(this);
						var $form = $this.closest('form');
						var ids = new Array();
						var i = 0;
						$this.attr("disabled", true);
						$('.selected-product').each(
								function() {
									$this = $(this);
									$selector = $this.children().first();
									if ($selector.prop("checked")) {
										var id = $(this).parent().children()
												.eq(3).html();
										ids[i] = id.replace(/(^\s*)|(\s*$)/g,
												"");// 先去除两边的空格
										i++;
									}
								});
						var search = window.location.search;
						search=decodeURI(search.substring(1));
						if ($("#update_active_date_checkbox").prop("checked")
								|| ids.length > 0) {
							$('#productIds').val(ids.join(','));
							$('#query').val(search);
							var ret = confirm("确认执行操作么？");
							if (ret) {
								BLC.ajax({
									url : $form.attr('action'),
									type : "POST",
									data : $form.serialize()
								});
								//$form.submit();
							}else{
								$('button.confirm-update-active-date').removeAttr("disabled");
							}
						} else {
							alert("请选择需要批处理的产品！");
							$('button.confirm-update-active-date').removeAttr("disabled");
						}
					});

			$('body').on('change', '#update_active_date_checkbox',
					function(event) {
						if ($("#update_active_date_checkbox").prop("checked")) {
							$value = $(".total-records").html();
							$('#maxIndex').val($value);
						}
					});

		});

function getModalSkeleton() {
	var $modal = $('<div>', {
		'class' : 'modal'
	});

	var $modalHeader = $('<div>', {
		'class' : 'modal-header'
	});
	$modalHeader.append($('<h3>'));
	$modalHeader.append($('<button>', {
		'class' : 'close',
		'data-dismiss' : 'modal',
		'html' : '&times;'
	}));
	$modal.append($modalHeader);

	var $modalBody = $('<div>', {
		'class' : 'modal-body'
	});
	$modal.append($modalBody);

	var $modalFooter = $('<div>', {
		'class' : 'modal-footer'
	});
	$modal.append($modalFooter);

	return $modal;
}
